import urllib.request


def get_course_data():
    # 1.要获取数据的网址
    data_url = "http://student.kaikeba.com/api/students/78087/courses?page=1&pagesize=12&onlyfree=0"

    # 2. 添加请求头
    user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"

    headers = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "max-age=0",
        "Connection": "keep-alive",
        "Cookie":"Hm_lvt_9a1843872729d95c5d7acbea784c51b2=1534941531; zg_did=%7B%22did%22%3A%20%2216561a50a311a2-0497b576095226-34677909-fa000-16561a50a323e8%22%7D; zg_9c90a84d308048688daeea828b2158aa=%7B%22sid%22%3A%201534941530683%2C%22updated%22%3A%201534941537175%2C%22info%22%3A%201534941530687%2C%22superProperty%22%3A%20%22%7B%7D%22%2C%22platform%22%3A%20%22%7B%7D%22%2C%22utm%22%3A%20%22%7B%7D%22%2C%22referrerDomain%22%3A%20%22www.baidu.com%22%2C%22landHref%22%3A%20%22https%3A%2F%2Fwww.kaikeba.com%2F%22%7D; UM_distinctid=16561a532bd79b-00ec08d7b50959-34677909-fa000-16561a532be26b; current=%7B%22name%22%3A%22Python%E7%88%AC%E8%99%AB%E5%95%86%E4%B8%9A%E9%A1%B9%E7%9B%AE%E7%8F%AD%20001%E6%9C%9F%22%2C%22courseid%22%3A89%7D; CNZZDATA1258741995=355454461-1534936691-%7C1534942265; Hm_lpvt_9a1843872729d95c5d7acbea784c51b2=1534942838; responseTimeline=94; user=%7B%22keep7day%22%3Afalse%2C%22qq%22%3A%222426395218%22%2C%22true_name%22%3A%22%E9%92%9F%E6%A2%93%E7%8F%91%22%2C%22mobile%22%3A%2213642326982%22%2C%22name%22%3A%22%E5%AD%A6%E5%91%98LhR993%22%2C%22id%22%3A78087%2C%22is_paid%22%3A1%2C%22avatar%22%3A%22http%3A%2F%2Fkkbconsole2.kaikeba.com%2Fstatics%2Fimages%2Favatar_100_100.png%22%2C%22token%22%3A%22e28d1ee1e2a14c529bbaa785461043ca%22%7D; zg_7c9bcf6917804ce5ad8448db3cbe3fb3=%7B%22sid%22%3A%201534941541.485%2C%22updated%22%3A%201534944295.914%2C%22info%22%3A%201534941541492%2C%22cuid%22%3A%20%22%22%7D",
        "Host": "student.kaikeba.com",
        "If-Modified-Since": "Mon, 11 Jun 2018 08:07:18 GMT",
        "If-None-Match": "W/5b1e2db6-a2a",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36",
        "X-Auth-Token": "e28d1ee1e2a14c529bbaa785461043ca"
    }
    # 3. 创建请求对象
    request = urllib.request.Request(data_url, headers=headers)
    # request.add_header("User-Agent", user_agent)

    # 5.手动的添加cookies
    # cookies = "Hm_lvt_9a1843872729d95c5d7acbea784c51b2=1534941531; zg_did=%7B%22did%22%3A%20%2216561a50a311a2-0497b576095226-34677909-fa000-16561a50a323e8%22%7D; zg_9c90a84d308048688daeea828b2158aa=%7B%22sid%22%3A%201534941530683%2C%22updated%22%3A%201534941537175%2C%22info%22%3A%201534941530687%2C%22superProperty%22%3A%20%22%7B%7D%22%2C%22platform%22%3A%20%22%7B%7D%22%2C%22utm%22%3A%20%22%7B%7D%22%2C%22referrerDomain%22%3A%20%22www.baidu.com%22%2C%22landHref%22%3A%20%22https%3A%2F%2Fwww.kaikeba.com%2F%22%7D; UM_distinctid=16561a532bd79b-00ec08d7b50959-34677909-fa000-16561a532be26b; current=%7B%22name%22%3A%22Python%E7%88%AC%E8%99%AB%E5%95%86%E4%B8%9A%E9%A1%B9%E7%9B%AE%E7%8F%AD%20001%E6%9C%9F%22%2C%22courseid%22%3A89%7D; CNZZDATA1258741995=355454461-1534936691-%7C1534942265; Hm_lpvt_9a1843872729d95c5d7acbea784c51b2=1534942266; user=%7B%22keep7day%22%3Afalse%2C%22qq%22%3A%222426395218%22%2C%22true_name%22%3A%22%E9%92%9F%E6%A2%93%E7%8F%91%22%2C%22mobile%22%3A%2213642326982%22%2C%22name%22%3A%22%E5%AD%A6%E5%91%98LhR993%22%2C%22id%22%3A78087%2C%22is_paid%22%3A1%2C%22avatar%22%3A%22http%3A%2F%2Fkkbconsole2.kaikeba.com%2Fstatics%2Fimages%2Favatar_100_100.png%22%2C%22token%22%3A%229ec3db1500044b939f2774223e6fdb4b%22%7D; zg_7c9bcf6917804ce5ad8448db3cbe3fb3=%7B%22sid%22%3A%201534941541.485%2C%22updated%22%3A%201534942651.888%2C%22info%22%3A%201534941541492%2C%22cuid%22%3A%20%22%22%7D; responseTimeline=177"
    # request.add_header('Cookie', cookies)

    # 4.请求数据 发送请求对象
    response = urllib.request.urlopen(request)

    # 5.保存数据
    data = response.read().decode('utf-8')

    with open('kaike.html', 'w', encoding='utf-8') as f:
        f.write(data)


get_course_data()
